package assignment1;


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
//import java.io.PrintWriter;
//import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import java.util.NoSuchElementException;
import java.util.Scanner;

class MyClient {
	
	private int _portnum = 7000;
	//private String _host = "155.98.111.60";		// used on CADE
	private String _host = "localhost";
	private Socket _sock=null; 
	
//	private BufferedReader _inBuffer = null;
//	private PrintWriter _outBuffer = null;
//	private OutputStreamWriter _outStream = null;
	
	DataOutputStream _dataOut = null;
	DataInputStream _dataIn = null;
	
	public MyClient() {
		
		try {
			InetAddress addr = InetAddress.getLocalHost(); 
			System.out.println("IP: " + addr.getHostAddress());
			
			_sock = new Socket(_host, _portnum);
			System.out.println("Connection to server established !");
			
		} catch (UnknownHostException e) {
			System.out.println("Unknown host " + _host);
			displayErrors(e);
			
		} catch (IOException e) {
			System.out.println("I/O error ");
			displayErrors(e);
		}
		
	}
	
	/**
	 * Displays connection error and exits program.
	 */
	private void displayErrors(Exception e) {
		
		System.err.println(e);
		System.exit(1);
	}

	/**
	 * 
	 */
	private void listenToServer() {
		
		try {
			//
			// input and output streams
//			_inBuffer = new BufferedReader(new InputStreamReader(_sock.getInputStream()));
//			_outStream = new OutputStreamWriter(_sock.getOutputStream(  ));
			//_outBuffer = new PrintWriter(_sock.getOutputStream(), true);
			//_outBuffer = new PrintWriter(new OutputStreamWriter(_sock.getOutputStream()),true);
			
			_dataIn = new DataInputStream(_sock.getInputStream());		// deprecated readLine()
			_dataOut = new DataOutputStream(_sock.getOutputStream());
		}
		catch (IOException ex) {
			System.out.println("getInput/OutputStream() failed at port " + _portnum);
		    System.exit(-1);
		}
		Scanner scanner = new Scanner(System.in);
		boolean quit=false;
		while (!quit) {			
			String line=null;
			try {				
				System.out.print("Enter data to send to server: ");
				line=scanner.nextLine();	// blocks until hit enter
				
				if (line != null) {
					try {
						_dataOut.writeUTF(line);
//						_dataOut.writeUTF(line + "\r\n");
						_dataOut.writeBoolean(true);
						_dataOut.writeUTF("192.168.1.1:8080");
						_dataOut.flush();
					}
					catch (IOException e) {
						System.out.println(e);
					}
					System.out.println("Client- " + line + " has been sent to server");
					if (line.toUpperCase().equals("QUIT")) {
						quit=true;
						break;			// exit program
					}
				}
				
				// wait till data is available from server
				while (_dataIn.available() == 0) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						System.out.println(e);
					}													
				}
				/* receive response from server */
				while (_dataIn.available() > 0) {					
					line = _dataIn.readUTF();
					System.out.println("Server sent: " + line);														
				}
				
			} catch (NoSuchElementException ex) {
				// empty input, wait for user input
				System.out.println("empty line.");
				
			} catch (IOException e) {
				System.out.println("Could not read from server.");
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		MyClient client = new MyClient();
		client.listenToServer();		
	}
}
